#include "Library.h"
#include <iostream>
#include <limits>
#include <iomanip>

using namespace library;

void printHeader() {
    system("clear");  // 清屏
    std::cout << R"(
    ================================================================
    |                    图 书 馆 管 理 系 统                        |
    ================================================================)" << std::endl;
}

void showMenu() {
    std::cout << "\n    ================================================================\n";
    std::cout << "    |                           功能菜单                            |\n";
    std::cout << "    ================================================================\n";
    std::cout << "    |                                                                |\n";
    std::cout << "    |  1. 显示所有图书            2. 借阅图书                       |\n";
    std::cout << "    |  3. 归还图书                4. 添加新图书                     |\n";
    std::cout << "    |  5. 删除图书                6. 编辑图书                       |\n";
    std::cout << "    |  7. 按库存排序              8. 统计信息                       |\n";
    std::cout << "    |  9. 退出系统                                                  |\n";
    std::cout << "    |                                                                |\n";
    std::cout << "    ================================================================\n\n";
    std::cout << "    请选择操作 (1-9): ";
}

void clearInputBuffer() {
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

void showOperationHeader(const std::string& title) {
    std::cout << "\n    ================================================================\n";
    std::cout << "    |                           " << title 
              << std::string(44 - title.length(), ' ') << "|\n";
    std::cout << "    ================================================================\n";
}

int main() {
    Library library("data/books.txt");
    int choice;
    
    while (true) {
        printHeader();
        showMenu();
        
        if (!(std::cin >> choice)) {
            std::cout << "    输入无效，请重试\n";
            clearInputBuffer();
            continue;
        }
        
        switch (choice) {
            case 1:
                showOperationHeader("图书列表");
                library.displayBooks();
                break;
            case 2: {
                showOperationHeader("借阅图书");
                int id, quantity;
                std::cout << "    请输入要借阅的图书ID: ";
                std::cin >> id;
                std::cout << "    请输入借阅数量: ";
                std::cin >> quantity;
                library.borrowBook(id, quantity);
                break;
            }
            case 3: {
                showOperationHeader("归还图书");
                int id, quantity;
                std::cout << "    请输入要归还的图书ID: ";
                std::cin >> id;
                std::cout << "    请输入归还数量: ";
                std::cin >> quantity;
                library.returnBook(id, quantity);
                break;
            }
            case 4: {
                showOperationHeader("添加新图书");
                std::string name, author;
                int stock;
                std::cout << "    请输入图书名称: ";
                clearInputBuffer();
                std::getline(std::cin, name);
                std::cout << "    请输入作者: ";
                std::getline(std::cin, author);
                std::cout << "    请输入库存数量: ";
                std::cin >> stock;
                library.addBook(name, author, stock);
                break;
            }
            case 5: {
                showOperationHeader("删除图书");
                int id;
                std::cout << "    请输入要删除的图书ID: ";
                std::cin >> id;
                library.deleteBook(id);
                break;
            }
            case 6: {
                showOperationHeader("编辑图书");
                int id, stock;
                std::string name, author;
                std::cout << "    请输入要编辑的图书ID: ";
                std::cin >> id;
                clearInputBuffer();
                std::cout << "    请输入新的图书名称: ";
                std::getline(std::cin, name);
                std::cout << "    请输入新的作者: ";
                std::getline(std::cin, author);
                std::cout << "    请输入新的库存数量: ";
                std::cin >> stock;
                library.editBook(id, name, author, stock);
                break;
            }
            case 7:
                showOperationHeader("按库存排序");
                library.sortByStock();
                break;
            case 8:
                showOperationHeader("统计信息");
                library.showStatistics();
                break;
            case 9:
                library.saveBooks();
                std::cout << "\n    感谢使用，再见！\n";
                return 0;
            default:
                std::cout << "    无效的选择，请重试\n";
        }
        
        std::cout << "\n    按回车键继续...";
        clearInputBuffer();
        std::cin.get();
    }
    
    return 0;
} 